package cn.cuilan.test.it.admin;

import cn.cuilan.entity.SysUser;
import cn.cuilan.repository.SysUserRepository;
import cn.cuilan.test.common.CommonTest;
import cn.cuilan.utils.Results;
import org.junit.Assert;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;

/**
 * SpringSecurity集成SysUser测试
 *
 * @author zhang.yan
 */
public class AdminSysUserControllerIT extends CommonTest {

    private final static String URL_PREFX = "/admin";

    @Autowired
    private SysUserRepository sysUserRepository;

    @Autowired
    private PasswordEncoder passwordEncoder;

    @Test
    public void loginIT() {
        SysUser sysUser = new SysUser();
        sysUser.setUsername("admin");
        sysUser.setPassword(passwordEncoder.encode("admin"));
        sysUserRepository.save(sysUser);

        MultiValueMap<String, String> valueMap = new LinkedMultiValueMap<>();
        valueMap.add("username", "admin");
        valueMap.add("password", "admin");
        Results results = testRestTemplate.postForObject(URL_PREFX + "/login", valueMap, Results.class);
        Assert.assertTrue(isSuccess(results));
    }

}
